﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Brain.Windows
{
    public partial class DockControl : UserControl
    {
        public bool Docked { get; set; }

        public Size LastSize;

        public int DockedArea;

        public PictureBox ResizePicture { get { return this.picture_Resize; } }

        [Description("The Title of this Dockable Control")]
        public string Title
        {
            get { return this.label_Title.Text; }
            set { this.label_Title.Text = value; }
        }

        public bool CanClose
        {
            get { return this.button_Close.Visible; }
            set { this.button_Close.Visible = value; }
        }

        public DockControl()
        {
            InitializeComponent();

            this.label_Title.MouseDown += new MouseEventHandler(panel_Head_MouseDown);
            this.label_Title.MouseMove += new MouseEventHandler(panel_Head_MouseMove);
            this.label_Title.MouseUp += new MouseEventHandler(panel_Head_MouseUp);
        }

        private Point Offset;
        private void panel_Head_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                Offset = new Point(e.X, e.Y);
            }
        }

        public void Dock(Rectangle docked)
        {
            this.Docked = true;
            this.Location = new Point(docked.X, docked.Y);
            this.Size = new Size(docked.Width, docked.Height);
        }

        private void panel_Head_MouseMove(object sender, MouseEventArgs e)
        {
            if (Offset != Point.Empty)
            {
                Point newLocation = this.Location;
                newLocation.X += e.X - Offset.X;
                newLocation.Y += e.Y - Offset.Y;
                this.Location = PointUtil.Clamp(newLocation, 
                    new Point(0, 0), 
                    new Point(this.Parent.Width - this.Width, this.Parent.Height - this.Height));

                IDocker docker = this.TopLevelControl as IDocker;
                if (docker != null && docker.DockManager != null)
                {
                    docker.DockManager.UserMovedControl(this);
                }
            }
        }

        private void panel_Head_MouseUp(object sender, MouseEventArgs e)
        {
            Offset = Point.Empty;
            if (!Docked)
            {
                LastSize = this.Size;
            }

            if (this.Parent is IDocker)
            {
                IDocker docker = this.Parent as IDocker;
                if (docker.DockManager != null)
                {
                    Rectangle? docked = docker.DockManager.GetRectangle(this, out this.DockedArea, true);
                    if (docked != null)
                    {
                        this.Docked = true;
                        this.Location = new Point(docked.Value.X, docked.Value.Y);
                        this.Size = new Size(docked.Value.Width, docked.Value.Height);
                    }
                    else
                    {
                        this.Docked = false;
                        this.Size = LastSize;
                    }
                }
            }
        }

        private void button_Close_Click(object sender, EventArgs e)
        {
            if (this.Parent.Controls.Contains(this))
            {
                this.Parent.Controls.Remove(this);
                this.Dispose();
            }
        }

        bool mouseClicked = false;
        private void picture_Resize_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                mouseClicked = true;
            }
        }
        private void picture_Resize_MouseUp(object sender, MouseEventArgs e)
        {
            mouseClicked = false;
        }

        private void picture_Resize_MouseMove(object sender, MouseEventArgs e)
        {
            if (mouseClicked)
            {
                int newHeight = picture_Resize.Top + e.Y;
                int newWidth = picture_Resize.Left + e.X;

                if (this.Docked && this.Parent is IDocker)
                {
                    IDocker docker = this.Parent as IDocker;
                    if (docker.DockManager != null)
                    {
                        if (docker.DockManager.CanResize(this, this.DockedArea, newWidth, newHeight))
                        {
                            this.Height = newHeight;
                            this.Width = newWidth;
                        }
                    }
                }
                else
                {
                    this.Height = newHeight;
                    this.Width = newWidth;
                }

                if (this.Docked)
                {
                     IDocker docker = this.Parent as IDocker;
                     if (docker != null && docker.DockManager != null)
                     {
                         docker.DockManager.Resized(this, DockedArea);
                     }
                }
            }
        }
    }
}
